home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / session.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-27  |  7.8 KB  |  486 lines

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "ttyio.h"
  6. #include "lutil.h"
  7. #include "xutil.h"
  8. #include "ftn.h"
  9. #include "nodelist.h"
  10. #include "statetbl.h"
  11. #include "session.h"
  12. #include "config.h"
  13. #include "emsi.h"
  14.  
  15. extern char *version;
  16. extern char *reldate;
  17. extern int nodeulock(faddr*);
  18.  
  19. node *nlent;
  20. fa_list *remote=NULL;
  21. int session_flags;
  22. int remote_flags;
  23.  
  24. int tx_define_type();
  25. int rx_define_type();
  26.  
  27. static int type;
  28. static char *data=NULL;
  29.  
  30. int session(a,nl,role,tp,dt)
  31. faddr *a;
  32. node *nl;
  33. int role;
  34. int tp;
  35. char *dt;
  36. {
  37.     int rc=1;
  38.     fa_list *tmpl;
  39.  
  40.     type=tp;
  41.     nlent=nl;
  42.  
  43.     debug(10,"start %s session type %d with %s",
  44.         role?"master":"slave",type,a?ascfnode(a,0x1f):"(none)");
  45.  
  46.     if (data) free(data);
  47.     data=NULL;
  48.     if (dt) data=xstrcpy(dt);
  49.  
  50.     emsi_local_protos=0;
  51.     emsi_local_opts=0;
  52.     emsi_local_lcodes=0;
  53.     
  54.     tidy_falist(&remote);
  55.     remote=NULL;
  56.     if (a)
  57.     {
  58.         remote=(fa_list*)xmalloc(sizeof(fa_list));
  59.         remote->next=NULL;
  60.         remote->addr=(faddr*)xmalloc(sizeof(faddr));
  61.         remote->addr->zone=a->zone;
  62.         remote->addr->net=a->net;
  63.         remote->addr->node=a->node;
  64.         remote->addr->point=a->point;
  65.         remote->addr->domain=xstrcpy(a->domain);
  66.         remote->addr->name=NULL;
  67.     }
  68.     else remote=NULL;
  69.  
  70.     remote_flags=0;
  71.  
  72.     if (role)
  73.     {
  74.         if (type == SESSION_UNKNOWN) (void)tx_define_type();
  75.         switch(type)
  76.         {
  77.         case SESSION_UNKNOWN:    rc=1; break;
  78.         case SESSION_FTSC:    rc=tx_ftsc(); break;
  79.         case SESSION_YOOHOO:    rc=tx_yoohoo(); break;
  80.         case SESSION_EMSI:    rc=tx_emsi(data); break;
  81.         }
  82.     }
  83.     else
  84.     {
  85.         if (type == SESSION_FTSC) session_flags |= FTSC_XMODEM_CRC;
  86.         if (type == SESSION_UNKNOWN) (void)rx_define_type();
  87.         switch(type)
  88.         {
  89.         case SESSION_UNKNOWN:    rc=1; break;
  90.         case SESSION_FTSC:    rc=rx_ftsc(); break;
  91.         case SESSION_YOOHOO:    rc=rx_yoohoo(); break;
  92.         case SESSION_EMSI:    rc=rx_emsi(data); break;
  93.         }
  94.     }
  95.     sleep(2);
  96.     for (tmpl=remote;tmpl;tmpl=tmpl->next)
  97.         (void)nodeulock(tmpl->addr);
  98.         /* unlock all systems, locks not owned by us are untouched */
  99.  
  100.     return rc;
  101. }
  102.  
  103. SM_DECL(tx_define_type,"tx_define_type")
  104. SM_STATES
  105.     skipjunk,wake,waitchar,nextchar,checkintro,sendinq
  106. SM_NAMES
  107.     "skipjunk","wake","waitchar","nextchar","checkintro","sendinq"
  108. SM_EDECL
  109.  
  110.     int c=0;
  111.     int count=0;
  112.     char buf[256],*p;
  113.     char ebuf[13],*ep;
  114.     int standby=0;
  115.  
  116.     int maybeftsc=0;
  117.     int maybeyoohoo=0;
  118.  
  119.     type=SESSION_UNKNOWN;
  120.     session_flags=0;
  121.     ebuf[0]='\0';
  122.     ep=ebuf;
  123.     buf[0]='\0';
  124.     p=buf;
  125.  
  126. SM_START(skipjunk)
  127.  
  128. SM_STATE(skipjunk)
  129.  
  130.     while ((c=GETCHAR(1)) >= 0) /*nothing*/ ;
  131.     if (c == TIMEOUT) {SM_PROCEED(wake);}
  132.     else {SM_ERROR;}
  133.  
  134. SM_STATE(wake)
  135.  
  136.     if (count++ > 20)
  137.     {
  138.         loginf("remote did not respond");
  139.         SM_ERROR;
  140.     }
  141.  
  142.     p=buf;
  143.     /*PUTSTR(" \r");*/
  144.     PUTCHAR('\r');
  145.     if ((c=GETCHAR(2)) == TIMEOUT)
  146.     {
  147.         SM_PROCEED(wake);
  148.     }
  149.     else if (c < 0)
  150.     {
  151.         loginf("error while waking remote");
  152.         SM_ERROR;
  153.     }
  154.     else
  155.     {
  156.         count=0;
  157.         SM_PROCEED(nextchar);
  158.     }
  159.  
  160. SM_STATE(waitchar)
  161.  
  162.     if ((c=GETCHAR(4)) == TIMEOUT)
  163.     {
  164.         standby=0;
  165.         ep=ebuf;
  166.         ebuf[0]='\0';
  167.         if (count++ > 8)
  168.         {
  169.             loginf("too many tries waking remote");
  170.             SM_ERROR;
  171.         }
  172.         SM_PROCEED(sendinq);
  173.     }
  174.     else if (c < 0)
  175.     {
  176.         loginf("error while getting intro from remote");
  177.         SM_ERROR;
  178.     }
  179.     else
  180.     {
  181.         SM_PROCEED(nextchar);
  182.     }
  183.  
  184. SM_STATE(nextchar)
  185.  
  186.     if (c == 'C')
  187.     {
  188.         session_flags |= FTSC_XMODEM_CRC;
  189.         maybeftsc++;
  190.     }
  191.     if (c == NAK)
  192.     {
  193.         session_flags &= ~FTSC_XMODEM_CRC;
  194.         maybeftsc++;
  195.     }
  196.     if (c == ENQ) maybeyoohoo++;
  197.  
  198.     if (((localoptions & NOWAZOO) == 0) &&
  199.         (maybeyoohoo > 1))
  200.     {
  201.         type=SESSION_YOOHOO;
  202.         SM_SUCCESS;
  203.     }
  204.  
  205.     if (maybeftsc > 1)
  206.     {
  207.         type=SESSION_FTSC;
  208.         SM_SUCCESS;
  209.     }
  210.  
  211.     if ((c >= ' ') && (c <= '~'))
  212.     {
  213.         if (c != 'C') maybeftsc=0;
  214.         maybeyoohoo=0;
  215.  
  216.         if ((p-buf) < (sizeof(buf)-1))
  217.         {
  218.             *p++=c;
  219.             *p='\0';
  220.         }
  221.  
  222.         if (c == '*')
  223.         {
  224.             standby=1;
  225.             ep=ebuf;
  226.             buf[0]='\0';
  227.         }
  228.         else if (standby)
  229.         {
  230.             if ((ep-ebuf) < (sizeof(ebuf)-1))
  231.             {
  232.                 *ep++=c;
  233.                 *ep='\0';
  234.             }
  235.             if ((ep-ebuf) >= (sizeof(ebuf)-1))
  236.             {
  237.                 standby=0;
  238.                 SM_PROCEED(checkintro);
  239.             }
  240.         }
  241.     }
  242.     else switch (c)
  243.     {
  244.     case DC1:    break;
  245.     case '\r':
  246.     case '\n':    standby=0;
  247.             ep=ebuf;
  248.             ebuf[0]='\0';
  249.             if (buf[0]) loginf("Intro: \"%s\"",buf);
  250.             p=buf;
  251.             buf[0]='\0';
  252.             break;
  253.     default:    standby=0;
  254.             ep=ebuf;
  255.             ebuf[0]='\0';
  256.             debug(10,"got '%s' reading intro",
  257.                 printablec(c));
  258.             break;
  259.     }
  260.  
  261.     SM_PROCEED(waitchar);
  262.  
  263. SM_STATE(checkintro)
  264.  
  265.     debug(10,"check \"%s\" for being EMSI request",ebuf);
  266.  
  267.     if (((localoptions & NOEMSI) == 0) &&
  268.         (strncasecmp(ebuf,"EMSI_REQA77E",12) == 0))
  269.     {
  270.         type=SESSION_EMSI;
  271.         data=xstrcpy("**EMSI_REQA77E");
  272.         PUTSTR("\r**EMSI_INQC816\r**EMSI_INQC816\r\021");
  273.         SM_SUCCESS;
  274.     }
  275.     else
  276.     {
  277.         p=buf;
  278.         buf[0]='\0';
  279.         SM_PROCEED(waitchar);
  280.     }
  281.  
  282. SM_STATE(sendinq)
  283.  
  284.     PUTCHAR(DC1);
  285.     if ((localoptions & NOEMSI) == 0)
  286.         PUTSTR("\r**EMSI_INQC816**EMSI_INQC816");
  287.     if ((localoptions & NOWAZOO) == 0)
  288.         PUTCHAR(YOOHOO);
  289.     PUTCHAR(TSYNC);
  290.     if ((localoptions & NOEMSI) == 0)
  291.         PUTSTR("\r\021");
  292.     SM_PROCEED(waitchar);
  293.  
  294. SM_END
  295. SM_RETURN
  296.  
  297.  
  298. SM_DECL(rx_define_type,"rx_define_type")
  299. SM_STATES
  300.     sendintro,waitchar,nextchar,checkemsi,getdat
  301. SM_NAMES
  302.     "sendintro","waitchar","nextchar","checkemsi","getdat"
  303. SM_EDECL
  304.  
  305.     int count=0;
  306.     int c=0;
  307.     int maybeftsc=0,maybeyoohoo=0;
  308.     char ebuf[13],*ep;
  309.     char *p;
  310.     int standby=0;
  311.     int datasize;
  312.  
  313.     type=SESSION_UNKNOWN;
  314.     session_flags|=FTSC_XMODEM_CRC;
  315.     ebuf[0]='\0';
  316.     ep=ebuf;
  317.  
  318.  
  319. SM_START(sendintro)
  320.  
  321. SM_STATE(sendintro)
  322.  
  323.     if (count++ > 8)
  324.     {
  325.         loginf("too many tries to get anything from the caller");
  326.         SM_ERROR;
  327.     }
  328.  
  329.     standby=0;
  330.     ep=ebuf;
  331.     ebuf[0]='\0';
  332.  
  333.     if ((localoptions & NOEMSI) == 0)
  334.     {
  335.         PUTSTR("**EMSI_REQA77E\r\021");
  336.     }
  337.     PUTSTR("\r\rAddress: ");
  338.     PUTSTR(ascfnode(whoami->addr,0x1f));
  339.     PUTSTR(" using ifcico ");
  340.     PUTSTR(version);
  341.     PUTSTR(" of ");
  342.     PUTSTR(reldate);
  343.     PUTCHAR('\r');
  344.     if (STATUS) {SM_ERROR;}
  345.     else {SM_PROCEED(waitchar);}
  346.  
  347. SM_STATE(waitchar)
  348.  
  349.     if ((c=GETCHAR(8)) == TIMEOUT)
  350.     {
  351.         SM_PROCEED(sendintro);
  352.     }
  353.     else if (c < 0)
  354.     {
  355.         loginf("error while getting from the caller");
  356.         SM_ERROR;
  357.     }
  358.     else
  359.     {
  360.         SM_PROCEED(nextchar);
  361.     }
  362.  
  363. SM_STATE(nextchar)
  364.  
  365.     if ((c >= ' ') && (c <= 'z'))
  366.     {
  367.         if (c == '*')
  368.         {
  369.             standby=1;
  370.             ep=ebuf;
  371.             ebuf[0]='\0';
  372.         }
  373.         else if (standby)
  374.         {
  375.             if ((ep-ebuf) < (sizeof(ebuf)-1))
  376.             {
  377.                 *ep++=c;
  378.                 *ep='\0';
  379.             }
  380.             if ((ep-ebuf) >= (sizeof(ebuf)-1))
  381.             {
  382.                 standby=0;
  383.                 SM_PROCEED(checkemsi);
  384.             }
  385.         }
  386.         SM_PROCEED(waitchar);
  387.     }
  388.     else switch (c)
  389.     {
  390.     case DC1:    SM_PROCEED(waitchar);
  391.             break;
  392.     case TSYNC:    standby=0;
  393.             ep=ebuf;
  394.             ebuf[0]='\0';
  395.             if (++maybeftsc > 1)
  396.             {
  397.                 type=SESSION_FTSC;
  398.                 SM_SUCCESS;
  399.             }
  400.             else {SM_PROCEED(waitchar);}
  401.             break;
  402.     case YOOHOO:    standby=0;
  403.             ep=ebuf;
  404.             ebuf[0]='\0';
  405.             if (++maybeyoohoo > 1)
  406.             {
  407.                 type=SESSION_YOOHOO;
  408.                 SM_SUCCESS;
  409.             }
  410.             else {SM_PROCEED(waitchar);}
  411.             break;
  412.     case '\r':
  413.     case '\n':    standby=0;
  414.             ep=ebuf;
  415.             ebuf[0]='\0';
  416.             if (ebuf[0]) {SM_PROCEED(checkemsi);}
  417.             else {SM_PROCEED(sendintro);}
  418.             break;
  419.     default:    standby=0;
  420.             ep=ebuf;
  421.             ebuf[0]='\0';
  422.             debug(10,"got '%s' from remote",
  423.                 printablec(c));
  424.             SM_PROCEED(waitchar);
  425.             break;
  426.     }
  427.  
  428. SM_STATE(checkemsi)
  429.  
  430.     debug(10,"check \"%s\" for being EMSI inquery or data",ebuf);
  431.  
  432.     if (localoptions & NOEMSI)
  433.     {
  434.         SM_PROCEED(sendintro);
  435.     }
  436.  
  437.     if (strncasecmp(ebuf,"EMSI_INQC816",12) == 0)
  438.     {
  439.         type=SESSION_EMSI;
  440.         data=xstrcpy("**EMSI_INQC816");
  441.         SM_SUCCESS;
  442.     }
  443.     else if (strncasecmp(ebuf,"EMSI_DAT",8) == 0)
  444.     {
  445.         SM_PROCEED(getdat);
  446.     }
  447.     else
  448.     {
  449.         SM_PROCEED(sendintro);
  450.     }
  451.  
  452. SM_STATE(getdat)
  453.  
  454.     debug(10,"try get emsi_dat packet starting with \"%s\"",ebuf);
  455.  
  456.     if (sscanf(ebuf+8,"%04x",&datasize) != 1)
  457.     {
  458.         SM_PROCEED(sendintro);
  459.     }
  460.  
  461.     datasize += 18; /* strlen("**EMSI_DATxxxxyyyy"), include CRC */
  462.     data=xmalloc(datasize+1);
  463.     strcpy(data,"**");
  464.     strcat(data,ebuf);
  465.     p=data+strlen(data);
  466.  
  467.     while (((p-data) < datasize) && ((c=GETCHAR(8)) >= 0))
  468.     {
  469.         *p++=c;
  470.         *p='\0';
  471.     }
  472.     if (c == TIMEOUT)
  473.     {
  474.         SM_PROCEED(sendintro);
  475.     }
  476.     else if (c < 0)
  477.     {
  478.         loginf("error while reading EMSI_DAT from the caller");
  479.         SM_ERROR;
  480.     }
  481.     type=SESSION_EMSI;
  482.     SM_SUCCESS;
  483.  
  484. SM_END
  485. SM_RETURN
  486.